<?php

namespace App\Http\Controllers\Api\V1\Vuls;

use App\Models\Vuls\Vuls;
use League\Fractal\TransformerAbstract;

class VulsTransformer extends TransformerAbstract
{
    //
    public function transform($lesson)
    {
        $lesson->statusNames = $this->getStatusName($lesson->status);
        return [
            'vuls_id' => $lesson->id,
            'vuls_title' => $lesson->title,
            'vuls_number' => $lesson->vuls_number,
            'vuls_reward' => $lesson->reward,
            'vuls_credit' => $lesson->credit,
            'vuls_status' => $lesson->status,
            'vuls_statusName' => $lesson->statusNames,
            'vuls_level' => $lesson->level_id,
            'vuls_content' => $lesson->content,
            'vuls_created_at' => $lesson->created_at,
        ];
    }

    /**
     * getStatusName 获取漏洞状态对应的名称
     *
     * @param $statusCode
     * @return mixed
     */
    private function  getStatusName ($statusCode){
        $status = Vuls::$vulsStatusMerge;
        $itemInfo = collect($status)->filter(function($item) use ($statusCode){
           if(in_array($statusCode, explode(',', $item['code']))){
               return $item;
           }
        })->pluck('name')->last();
        return $itemInfo;
    }
}
